package com.zrgg.futuwen.wxapi;

import android.content.Context;

import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

/**
 * Created by xcc on 2017/2/21
 * 微信支付调用.
 */
public class PayTools {
    private Context context;
    private IWXAPI api;
    private PayReq req;
    private PayMode payMode;

    public PayTools(Context context) {
        this.context = context;
    }

    public void payStart(PayMode payMode) {
        this.payMode = payMode;

        WChatConstants.APP_ID = payMode.appid;

        api = WXAPIFactory.createWXAPI(context, null);
        // 将该app注册到微信
        api.registerApp(payMode.appid);

        genPayReq();
        sendPayReq();
    }

    private void genPayReq() {
        req = new PayReq();
        req.appId = payMode.appid;
        req.partnerId = payMode.partnerid;
        req.prepayId = payMode.prepayid;
        req.packageValue = payMode.packageX;
        req.nonceStr = payMode.noncestr;
        req.timeStamp = payMode.timestamp;
        req.sign = payMode.sign;
    }

    private void sendPayReq() {
        api.registerApp(payMode.appid);
        api.sendReq(req);
    }
}
